The goal of this session is to get to started using RStudio, learn how to use variables and solve basic calculations in R. We will cover:
One hour exercise based session with tutor support. You will be given example code for a problem, then given a related exercise to complete.
R is a popular language, especially in data science, this can be seen in the TIOBE Index for August 2020.
It has lots of strengths:
RStudio by default has four main quadrants as shown below. The layout is customisable, as is the background.
When you load RStudio the syntax editor will not be open. Try and open one just like as shown below.
For these workshops we will be using R Markdown. It allows you to have text (with simple formatting) and chunks of R code.
To run code in a code chunk either press the green play button or press Ctrl + Enter (or Cmd + Enter on Mac).
We can use R to do simple or advanced calculations for us. Remember to run the code press the green play button or press Ctrl + Enter (or Cmd + Enter on Mac).
7 * 6## [1] 42
5 / (2^2)## [1] 1.25
(16 - 4) + (1 * 9)## [1] 21
Use R to work out the following arithmetic:
# type your code hereA variable is a named storage of information. In our case today we are storing numbers.
We can assign variables by using <-. You should see the variable appear to your right in the global enviroment once you’ve run this command.
height <- 155We can then print the output of the variable by typing in its name.
height## [1] 155
When calling a variable, be careful to type it exactly (you can also copy it or use code completion to help).
Try calling the height variable, but spell it incorrectly. You should get an error with something like Error: object ‘hieght’ not found
# type incorrect height call hereWe can do calculations on these variables, just as we did before. We first assign the variables, then use them in the calculation.
# test scores
Score1 <- 42
Score2 <- 92
Score3 <- 68
# average score calculation
AveScore <- (Score1+Score2+Score3)/3
# print average score
AveScore## [1] 67.33333
You will have noticed the hashtags (#) with text in the above example. These are called comments. In later R sessions we will use a lot of comments to tell us (and others) what each line or section of code is doing.
# this is a commentYou can also change the value of a variable you have already assigned. Here we are going to add our new pay check to our previous bank balance.
Run this code to test it out:
# create variables
BankBalance <- 100
PayCheck <- 250
# add old bank balance and pay check, assigning result to bank balance
BankBalance <- BankBalance + PayCheck
# print bank balance
BankBalance## [1] 350
Try and add another pay check of £50 to the bank balance variable.
hint: if this doesn’t work run the code chunk above
# Reassigning variables taskUse R to work out a body mass index (BMI) of someone who is 79kg, and 1.77m tall.
# Exercise: BMI calculationDebug the code below that is finding the weighted average of a students coursework and exam scores. You should find three errors:
# Exercise: weighted average debugging
exam1 <- 52
coursework1 <- 82
exam2 <- 78
coursework2 < 48
cw_weight <- 0.4
ex_weight <- 0.6
course1 <- (exam1 * ex_weight) + (coursework * cw_weight)
course2 <- (exam2 * ex_weight) + (coursework2 * cw_weight)
overall_grade <- (course1 + course2)/3
overall_gradeRobin and Charlie are a married couple, one gets paid an hourly rate, the other has an annual salary. They want to workout how much annual salary they have combined before tax. Out of interest Charlie also wants to know what her hourly rate is before tax.
They used simple calculations using the following formulas:
(number hours worked per week x hourly rate) x number of weeks worked = annual salary (annual salary ÷ number of weeks in a year) ÷ hours worked per week = hourly rate
Re-arrange the code so the calculations run. You should have both the combined salary and Charlies hourly rate calculations printed.
# weeks in year
weeksYear <- 52
# Charlies hourly rate
Charlie_HourlyRate <- (Charlie_annualSary / weeksYear)/Charlie_hoursPerWeek
Charlie_HourlyRate
# salaries
Robin_HourlyRate <- 16.5
Charlie_annualSary <- 31800
# Combined salaries
CombinedSalaries <- Robin_annualSalary + Charlie_annualSary
CombinedSalaries
# Robins annual salary
Robin_annualSalary <- (Robin_hoursPerWeek * Robin_HourlyRate) * Robin_weeksWorking
# hours worked
Robin_hoursPerWeek <- 25
Robin_weeksWorking <- 48
Charlie_hoursPerWeek <- 35This is the first time that we are exploring a remote learning format for our workshops and we would be grateful if you could take 2 mins before the end of the workshop to get your feedback!
The solutions we be available from a link at the end of the survey.
A take home coding task for you.
Task: Splitting a Pizza Pilgrims restaurant bill between 3 friends; Roger, Amal and Genevieve.
# individual coding challengeRecommended for more information on the RStudio environment: https://rladiessydney.org/courses/ryouwithme/01-basicbasics-1/
Recommended for more information on using R Markdown: https://rmarkdown.rstudio.com/lesson-1.html